import {
    resources,
    excelUtil as ExcelUtil
} from '../../../../../util';
import { account as AccountApi, excel as ExcelApi} from '../../../../../config/request.js';

module.exports = {
    name: 'list',
    data() {
        return {
            downloadName: '用户信息-' + new Date().getTime() + '.xlsx',
            // tableData: allTableData.slice(0, SHOW_COUNT_LESS),
            fields: resources.fields,
            tableCount: resources.tableCount,
            selectCity: '',
            tableData: [],
            layout: resources.layout,
            currentPage : 1,
            total:0,
            token:null
        }
    },
    methods: {
        downloadExlDetail() {
            ExcelApi.getExcelData.call(this, data => {
                this._formatterData(data['users'])
            })
        },_formatterData(users){
            this.downloadName = '用户信息-' + new Date().getTime() + '.xlsx';
            const data = ExcelUtil.formatterExcelData(users)
            this.generateExl(this.downloadName, data);
        },
        downloadExl() {
            this.downloadName = '用户信息-' + new Date().getTime() + '.xlsx';
            var tableData = this.$store.state.users.tableData;
            var data = ExcelUtil.formatterData(tableData, ['status', 'icon', 'used_days', 'notification_status', 'optional_status', 'showed_firstbox']);
            this.generateExl(this.downloadName, data);
        },
        handleCurrentChange(val) {
            this.currentPage = val
            this.getUsers()
        },
        downloadExlAll() {
            UserApi.getAllUsers.call(this, data => {
                var tableData = data['data'];
                if (!tableData) {
                    return;
                }
                tableData.forEach(user => {
                   var date = new Date(user.createdAt);
                   Object.assign(user,{joinTime : date.getTime() / 1000})
                });
                var data = ExcelUtil.formatterData(tableData, ['status', 'icon', 'used_days', 'notification_status', 'optional_status', 'showed_firstbox']);
                console.log(data)
                this.generateExl('全部用户信息-' + new Date().getTime() + '.xlsx', data);
            })
        },
        generateExl(name, data) {
            ExcelUtil.downloadExl(name, data);
        },
        formatterCtime(item) {
            return resources.formatterTime( item.ctim )
        },
        /**
         * 获取用户信息
         * 
         */
        getUsers() {
            var page = this.currentPage

            var that = this;
            var token = this.token
            AccountApi.userList.call(this, {
                token,
                page
            }, data => {
                that.total = data.data.count
                that.tableData = data.data.lit
            });
        },
        checkLevel() {
            var user = this.$store.state.user.userinfo;
            return user && user.level == 1;
        },
        formatterCtime(item) {
            return new Date(item.ctime * 1000).toLocaleString();
        }
    },
    created() {
        this.token = this.$store.state.user.userinfo.token
        this.getUsers();
    },
    mounted() {}
}